<?php
/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @author Eddie Drapkin <eddie@frameweld.com>
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt
 * @package Solphr
 * @version 0.1-pre
 */

require_once dirname(__FILE__) . '/Solphr_Exception.php';
require_once dirname(__FILE__) . '/Request/Solphr_Request_Interface.php';
require_once dirname(__FILE__) . '/Request/Solphr_Request_Curl.php';
require_once dirname(__FILE__) . '/Solphr_SimpleAction.php';
require_once dirname(__FILE__) . '/Query/Solphr_Query_Interface.php';
require_once dirname(__FILE__) . '/Query/Solphr_Query_DateRange.php';
require_once dirname(__FILE__) . '/Query/Solphr_Query_Raw.php';
require_once dirname(__FILE__) . '/Query/Solphr_Query_Term.php';

/**
 * DOCUMENT ME!
 * @author Eddie Drapkin <eddie@frameweld.com>
 * @license http://www.gnu.org/licenses/lgpl-3.0.txt
 * @package Solphr
 */
class Solphr_Delete extends Solphr_SimpleAction {
	private $IDs = array();

	public function addID($ID) {
		$this->IDs[] = (int)$ID;
	}
	
	public function executeById() {
		if(empty($this->IDs)) {
			throw new Solphr_Exception('Cannot execute an empty Solr transaction');
		}
		
		$request = '<delete>';
		
		foreach($this->IDs as $id) {
			$request .= "<id>$id</id>";
		}
		
		$request .= '</delete>';
		
		$this->req->setDataFromString($request, true);
		$this->IDs = array();
		return $this->req->commit();
	}
	
	public function addQuery(Solphr_Query_Interface $query) {
		$this->queryItems[] = (string)$query;
	}
	
	public function executeByQuery() {
		if(empty($this->queryItems)) {
			throw new Solphr_Exception('Cannot execute an empty Solr transaction');
		}
		
		$request = '<delete>';
		
		foreach($this->queryItems as $q) {
			$q = call_user_func(array(Solphr_Request_Interface::IMPL, 'queryEscape'), $q);
			$request .= "<query>" . implode(' ', $q) . "</query>";
		}
		
		$request .= '</delete>';
		
		$this->req->setDataFromString($request, true);
		$this->queryItems = array();
		return $this->req->commit();
	}
}